package xcmg.device.web.delivery;

import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import xcmg.device.infra.BasicController;
import xcmg.device.infra.ErrorCode;
import xcmg.device.service.delivery.LogisticsBasicDataService;
import xcmg.device.service.vo.delivery.LogisticsBasicDataVO;
import yb.ecp.fast.infra.annotation.FastMappingInfo;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.infra.infra.PageCommonVO;
import yb.ecp.fast.infra.infra.SearchCommonVO;

import java.util.Arrays;

/**
 * 物流基础数据维护Controller
 * @author huhuawei
 */
@RestController
@RequestMapping("/logisticsBasic")
public class LogisticsBasicDataController extends BasicController{

    @Autowired
    private LogisticsBasicDataService logisticsBasicDataService;

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ApiOperation(value = "增加物流基础信息")
    @FastMappingInfo(needLogin = true)
    public ActionResult add(@RequestBody LogisticsBasicDataVO logisticsBasicDataVO)throws Exception{
        //如果物流信息对象为空
        if(logisticsBasicDataVO == null){
            return actionResult(ErrorCode.IllegalArument);
        }
        //设置当前的UserID
        logisticsBasicDataVO.setCreateId(getUserId());
        logisticsBasicDataVO.setOrgId(getOrgId());
        return actionResult(logisticsBasicDataService.add(logisticsBasicDataVO));
    }

    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    @ApiOperation(value = "删除物流基础信息")
    @FastMappingInfo(needLogin = true)
    public ActionResult remove(@RequestBody LogisticsBasicDataVO logisticsBasicDataVO){
        String [] ids = logisticsBasicDataVO.getIds();
        //校验需要删除的ID
        if(ids==null||ids.length==0){
            return actionResult(ErrorCode.IllegalArument);
        }
        ErrorCode ret=logisticsBasicDataService.remove(Arrays.asList(ids));
        return actionResult(ret);
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ApiOperation(value = "更新物流基础信息")
    @FastMappingInfo(needLogin = true)
    public ActionResult update(@RequestBody LogisticsBasicDataVO logisticsBasicDataVO)throws Exception{
        //如果传入更新的实体类为空
        if(logisticsBasicDataVO == null || StringUtils.isEmpty(logisticsBasicDataVO.getId())){
            return actionResult(ErrorCode.IllegalArument);
        }
        //设置当前的UserID
        logisticsBasicDataVO.setCreateId(getUserId());
        logisticsBasicDataVO.setOrgId(getOrgId());
        return actionResult(logisticsBasicDataService.update(logisticsBasicDataVO));
    }

    @RequestMapping(value = "/list",method = RequestMethod.POST)
    @ApiOperation(value = "物流基础信息列表")
    @FastMappingInfo(needLogin = true)
    public ActionResult listDeliveryNote(@RequestBody SearchCommonVO<LogisticsBasicDataVO> condition) throws Exception {
    if(condition==null){
        return actionResult(ErrorCode.IllegalArument);
    }
    LogisticsBasicDataVO vo = condition.getFilters();
    if(vo==null){
       vo=new LogisticsBasicDataVO();
    }
    //设置只能查看当前企业的id
    vo.setOrgId(getOrgId());
    condition.setFilters(vo);
    PageCommonVO pageCommonVO=logisticsBasicDataService.list(condition);
    return actionValueResult(pageCommonVO.getPageInfo());
    }

    /**
     * 根据ID查询物流基础信息
     * @param id
     * @return
     */
    @RequestMapping(value = "/item",method = RequestMethod.GET)
    @ApiOperation(value = "物流基础信息")
    @FastMappingInfo(needLogin = true)
    public ActionResult item(String id){
        if(StringUtils.isEmpty(id)){
            return actionResult(ErrorCode.IllegalArument);
        }
        return actionResult(ErrorCode.Success,logisticsBasicDataService.item(id));
    }
}
